/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "hmi_theme.h"
#include "hmi_asset.h"
#include "hmi_subject.h"
#include "hmi_pg_navigation.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void back_btn_ev_cb(lv_event_t *e);
static void del_ev_cb(lv_event_t *e);

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_font_t *top_bar_font;
static lv_anim_timeline_t *timeline;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
lv_obj_t *hmi_pg_top_bar_create(lv_obj_t *parent, const char *pg_name)
{
    lv_obj_t *top_bar = lv_obj_create(parent);
    lv_obj_remove_style_all(top_bar);
    lv_obj_add_style(top_bar, &hmi_g_wrap_style, 0);
    lv_obj_set_size(top_bar, lv_pct(100), lv_pct(20));

    lv_obj_t *back_btn = lv_obj_create(top_bar);
    lv_obj_remove_style_all(back_btn);
    lv_obj_set_height(back_btn, lv_pct(80));
    lv_obj_update_layout(back_btn);
    lv_obj_set_width(back_btn, lv_obj_get_height(back_btn));
    lv_obj_set_style_radius(back_btn, LV_RADIUS_CIRCLE, 0);
    lv_obj_set_style_bg_color(back_btn, lv_palette_main(LV_PALETTE_GREY), 0);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_30, 0);
    lv_obj_align(back_btn, LV_ALIGN_LEFT_MID, 10, 0);
    lv_obj_add_event_cb(back_btn, back_btn_ev_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(back_btn, del_ev_cb, LV_EVENT_DELETE, NULL);

    lv_obj_t *icon = lv_image_create(back_btn);
    lv_image_set_src(icon, hmi_png("arrow_left"));
    lv_obj_set_style_image_recolor_opa(icon, LV_OPA_COVER, 0);
    lv_obj_add_style(icon, &hmi_g_icon_style, 0);
    lv_obj_center(icon);

    lv_obj_update_layout(top_bar);
    int32_t font_height = lv_obj_get_height(top_bar) * 0.5;
    if (!top_bar_font)
        top_bar_font = hmi_ttf_create_static("def_font", font_height, 0);

    lv_obj_t *title = lv_label_create(top_bar);
    lv_obj_set_style_text_font(title, top_bar_font, 0);
    lv_label_set_text(title, lv_tr(pg_name));
    lv_obj_align_to(title, top_bar, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(title, del_ev_cb, LV_EVENT_DELETE, NULL);

    return top_bar;
}

void *hmi_pg_top_bar_anim(lv_obj_t *top_bar)
{
    LV_ASSERT_NULL(top_bar);

    lv_obj_t *back_btn = lv_obj_get_child(top_bar, 0);
    lv_obj_t *title = lv_obj_get_child(top_bar, -1);

    lv_obj_update_layout(back_btn);
    lv_obj_update_layout(title);
    int32_t back_btn_x = lv_obj_get_x(back_btn);
    int32_t title_x = lv_obj_get_x(title);

    lv_anim_t a1, a2;

    lv_anim_init(&a1);
    lv_anim_set_var(&a1, back_btn);
    lv_anim_set_values(&a1, back_btn_x + 100, back_btn_x);
    lv_anim_set_exec_cb(&a1, (lv_anim_exec_xcb_t)lv_obj_set_x);
    lv_anim_set_path_cb(&a1, lv_anim_path_ease_out);
    lv_anim_set_time(&a1, 500 * hmi_g_anim_rate);

    lv_anim_init(&a2);
    lv_anim_set_var(&a2, title);
    lv_anim_set_values(&a2, title_x + 100, title_x);
    lv_anim_set_exec_cb(&a2, (lv_anim_exec_xcb_t)lv_obj_set_x);
    lv_anim_set_path_cb(&a2, lv_anim_path_ease_out);
    lv_anim_set_time(&a2, 500 * hmi_g_anim_rate);

    if (timeline)
    {
        lv_anim_timeline_delete(timeline);
        timeline = NULL;
    }

    timeline = lv_anim_timeline_create();
    lv_anim_timeline_add(timeline, 200, &a1);
    lv_anim_timeline_add(timeline, 400, &a2);
    lv_anim_timeline_start(timeline);

    lv_obj_fade_in(back_btn, 1000 * hmi_g_anim_rate, 0);
    lv_obj_fade_in(title, 1000 * hmi_g_anim_rate, 0);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void back_btn_ev_cb(lv_event_t *e)
{
    LV_UNUSED(e);

    hmi_pg_pop();
}

static void del_ev_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_current_target(e);

    if (timeline)
    {
        lv_anim_timeline_delete(timeline);
        timeline = NULL;
    }
    lv_anim_del(obj, NULL);
}
